{% extends "index.html" %}
{% block title %}
    修改图书
{% endblock %}
{% block content-title %}
    修改图书
{% endblock %}
{% block content %}
    <div class="form-group">
        书名<input type="text" name="title" id="title" value="{{ current_book.title }}" class="form-control">
    </div>
    <div class="form-group">
        价格<input type="text" name="price" id="price" value="{{ current_book.price }}" class="form-control">
    </div>
    <div class="form-group">
        出版日期 <input type="date" name="publish_date" id="publish_date"
                        value="{{ current_book.publish_date|date:'Y-m-d' }}"
                        class="form-control">
    </div>
    <div class="form-group">
        出版社<select name="publish_id" id="publish_id" class="form-control">
        {% for publish in publish_queryset %}
            {% if publish == current_book.publish %}
                <option value="{{ publish.pk }}" selected>{{ publish.name }}</option>
            {% else %}
                <option value="{{ publish.pk }}">{{ publish.name }}</option>
            {% endif %}
        {% endfor %}
    </select>
    </div>
    <div class="form-group">
        作者<select name="authors" id="authors" class="form-control" multiple>
        {% for author in author_queryset %}
            {% if author in current_book.authors.all %}
                <option value="{{ author.pk }}" selected>{{ author.name }}</option>
            {% else %}
                <option value="{{ author.pk }}">{{ author.name }}</option>
            {% endif %}
        {% endfor %}
    </select>
    </div>
    <div class="form-group">
        <button type="submit" value="修改" class="btn btn-success btn-block">修改</button>
    </div>
    <div class="form-group">
        <a type="button" href="/book/list"
           class="btn btn-info btn-block">返回
        </a>
    </div>
{% endblock %}
{% block js %}
    <script>
        $('button').click(function () {
            $.ajax({
                url: '',
                type: 'post',
                data: {
                    title: $('#title').val(),
                    price: $('#price').val(),
                    publish_date: $('#publish_date').val(),
                    publish_id: $('#publish_id').val(),
                    authors: $('#authors').val(),
                },
                dataType: 'json',
                success: function (res) {
                    if (res.code == 200) {
                        location.replace('/book/list')
                    }
                }
            })
        })
    </script>
{% endblock %}